<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <meta charset="UTF-8"/>
        <title>consumersJwt</title>
    </head>

    <body>
        <div th:include="top :: html"></div>
        <div class="contentDiv">

            <table class="table table-hover table-condensed">
                <legend>
                    <strong>/kong/consumers/<a th:text="${consumer.username}"></a>/Jwts</strong>
                </legend>
                <thead>
                    <tr>
                        <th>id</th>
                        <th>consumer_id</th>
                        <th>key</th>
                        <th>secret</th>
                        <th>algorithm</th>
                        <th>created_at</th>
                        <th>operate</th>
                    </tr>
                </thead>
                <tbody>
                    <tr th:each="jwt : ${jwts}">
                        <th scope="row" th:text="${jwt.id}"></th>
                        <td th:text="${consumer.id}"></td>
                        <td th:text="${jwt.key}"></td>
                        <td th:text="${jwt.secret}"></td>
                        <td th:text="${jwt.algorithm}"></td>
                        <td th:text="${#dates.format(jwt.created_at * 1000, 'yyyy-MM-dd HH:mm:ss')}"></td>
                        <td>
                            <a class="btn btn-danger btn-delete" href="javascript:void(0)" th:consumerId="${consumer.id}" th:jwt="${jwt.id}">delete</a>
                        </td>
                    </tr>
                </tbody>
            </table>

            <div><a class="btn btn-primary btn-add" href="javascript:void(0)" role="button" th:consumerId="${consumer.id}">add jwt</a></div>
        </div>
        <script type="application/javascript">
            $(function(){
                $('.btn-add').click(function(){
                    var consumerId = $(this).attr('consumerId');
                    var url = "/kong/consumers/" + consumerId + "/jwt";
                    $.ajax({
                        url: url,
                        async: false,
                        type: "post",
                        success: function(){
                            window.location.reload();
                        },
                        error: function(e){
                            alert(e.message);
                            window.location.reload();
                        }
                    })
                });
                $('.btn-delete').click(function(){
                    if(!confirm("Make sure to delete this?")){
                        return;
                    }
                    var consumerId = $(this).attr('consumerId');
                    var jwt_id = $(this).attr('jwt');
                    var url = "/kong/consumers/" + consumerId + "/jwt/" + jwt_id;
                    $.ajax({
                        url: url,
                        async: false,
                        type: "delete",
                        success: function(){
                            window.location.reload();
                        },
                        error: function(e){
                            alert(e.message);
                            window.location.reload();
                        }
                    })
                });
            })
        </script>
    </body>
</html>